package com.my.dao.impl;

import java.math.BigInteger;
import java.util.List;

import com.my.bean.OrderAccepted;
import com.my.dao.IBaseDAO;
import com.my.dao.OrderAcceptedDAO;

public class OrderAcceptedDAOImpl implements OrderAcceptedDAO {
	private IBaseDAO<OrderAccepted> basedao;

	public void setBasedao(IBaseDAO<OrderAccepted> basedao) {
		this.basedao = basedao;
	}

	@Override
	public boolean addOrderAccepted(OrderAccepted orderaccepted)
			throws Exception {
		if (this.checkOrderAccepted(orderaccepted)) {
			return basedao.add(orderaccepted);
		}
		return false;
	}

	@Override
	public OrderAccepted getOrderAcceptedById(Class<OrderAccepted> c, Integer id)
			throws Exception {
		return basedao.getById(c, id);
	}

	@Override
	public BigInteger getTotalOrderAcceptedRecords() throws Exception {
		String sql = "select count(order_accepted_id) from info_order_accepted where state='未反馈'";
		return basedao.getTotalRecords(sql);
	}

	@Override
	public List<OrderAccepted> getAllOrderAcceptedByPage(int pageindex,
			int pagesize) throws Exception {
		String hql = "from OrderAccepted as o where o.state='未反馈'";
		return basedao.getAllEntityByPage(hql, pageindex, pagesize);
	}

	@Override
	public BigInteger searchTotalOrderAcceptedRecords(
			OrderAccepted orderaccepted) throws Exception {
		String sql = "select count(a.order_accepted_id) from info_order_accepted a inner join info_order b on b.order_id=a.order_id inner join info_user c on c.user_id=b.user_id where c.province like ? and c.city like ? and b.order_no like ? and a.state='未反馈'";
		String item1 = orderaccepted.getOrder().getUser().getProvince() + "%";
		String item2 = orderaccepted.getOrder().getUser().getCity() + "%";
		String item3 = orderaccepted.getOrder().getNo() + "%";
		return basedao.getTotalRecords(sql, item1, item2, item3);
	}

	@Override
	public List<OrderAccepted> searchOrderAcceptedByPage(int pageindex,
			int pagesize, OrderAccepted orderaccepted) throws Exception {
		String hql = "from OrderAccepted as o where o.order.user.province like ? and o.order.user.city like ? and o.order.no like ? and o.state='未反馈'";
		String item1 = orderaccepted.getOrder().getUser().getProvince() + "%";
		String item2 = orderaccepted.getOrder().getUser().getCity() + "%";
		String item3 = orderaccepted.getOrder().getNo() + "%";
		return basedao.getAllEntityByPage(hql, pageindex, pagesize, item1,
				item2, item3);
	}

	boolean checkOrderAccepted(OrderAccepted orderaccepted) {
		if (orderaccepted == null || orderaccepted.getOrder() == null) {
			return false;
		} else {
			return true;
		}
	}

}
